package com.cb.kfc.service.impl;

import com.cb.kfc.dao.IRoleDao;
import com.cb.kfc.domain.Permission;
import com.cb.kfc.domain.Role;
import com.cb.kfc.service.IRoleService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private IRoleDao roleDao;

    @Override
    public void save(Role role) {
        roleDao.save(role);
    }

    @Override
    public List<Role> findAll(int page,int size) throws Exception {
        PageHelper.startPage(page,size);
        return roleDao.findAll();
    }
    /**
     * 获得用户的id，批量，或者单个删除角色
     */
    public Integer deleteRoleById(String[] ids) {
        if(ids !=null && ids.length>0){
            Integer integer = roleDao.deleteUR(ids);
            integer = roleDao.deleteUP(ids);
            integer = roleDao.deleteR(ids);
            return integer;
        }
        return 0;
    }

        @Override
        public void addPermissionToRole(String roleId, String[] permissionIds) {
            for(String permissionId:permissionIds){
                roleDao.addPermissionToRole(roleId,permissionId);
            }
        }

        @Override
        public Role findById(String roleId) {
            return roleDao.findById(roleId);
        }

        @Override
        public List<Permission> findOtherPermissions(String roleId) {
            return roleDao.findOtherPermissions(roleId);
        }
    }

